package com.example.store2.Service.impl;

import com.example.store2.Mapper.AttenMapper;
import com.example.store2.Mapper.AttentionMapper;
import com.example.store2.Mapper.PublicResourcesMapper;
import com.example.store2.Mapper.URMapper;
import com.example.store2.Service.PublicResourcesService;
import com.example.store2.pojo.Atten;
import com.example.store2.pojo.PageBean;
import com.example.store2.pojo.PublicResources;
import com.example.store2.pojo.Resources;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PublicResourcesServiceImpl implements PublicResourcesService {
    @Autowired
    private PublicResourcesMapper publicResourcesMapper;
    @Autowired
    private URMapper urMapper;
    @Autowired
    private AttenMapper attenMapper;

    @Override
    public boolean add(Integer userId, Integer resourcesId) {
        if(urMapper.countadd(userId,resourcesId)!=0){
            return false;
        }
        else{
            Resources resources=new Resources();
            resources.setUserId(userId);
            resources.setId(resourcesId);
            resources.setState("他人");
            urMapper.add(resources);
            return true;
        }
    }

    @Override
    public PageBean list(Integer page, Integer pageSize, Integer occupationId, Integer classesId, String resourcesName) {
        PageHelper.startPage(page,pageSize);
        List<PublicResources> e=publicResourcesMapper.list(occupationId,classesId,resourcesName);
        Page<PublicResources> p=(Page<PublicResources>) e;
        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    @Override
    public boolean addattention(Atten atten) {
        Integer userId=atten.getUserId();
        Integer beuserId=atten.getBeuserId();
        if(attenMapper.find(userId,beuserId)!=0)
            return false;//说明这个人已经关注过了
        else {
            attenMapper.add(userId,beuserId);
            return true;
        }
    }
}
